package com.casic.ops.entity.vo;

import com.casic.ops.consts.history.HistoryOperator;
import com.casic.ops.entity.domain.HistoryValueSnapshotDO;
import com.orion.utils.convert.TypeStore;
import com.orion.utils.time.Dates;
import lombok.Data;

import java.util.Date;

/**
 * @ClassName ops-platform
 * @Description  历史值快照vo
 * @Author mianbin
 * @Date 2022/5/31 10:59
 * @Version 1.0
 **/
@Data
public class HistoryValueVO {

    /**
     * id
     */
    private Long id;

    /**
     * 操作类型 1新增 2修改 3删除
     *
     * @see HistoryOperator
     */
    private Integer type;

    /**
     * 原始值
     */
    private String beforeValue;

    /**
     * 新值
     */
    private String afterValue;

    /**
     * 修改人id
     */
    private Long updateUserId;

    /**
     * 修改人用户名
     */
    private String updateUserName;

    /**
     * 修改时间
     */
    private Date createTime;

    /**
     * 修改时间
     */
    private String createTimeAgo;

    public static HistoryValueVO historyValueSnapshotDO2historyValueVO(HistoryValueSnapshotDO p) {
        HistoryValueVO vo = new HistoryValueVO();
        vo.setId(p.getId());
        vo.setType(p.getOperatorType());
        vo.setBeforeValue(p.getBeforeValue());
        vo.setAfterValue(p.getAfterValue());
        vo.setUpdateUserId(p.getUpdateUserId());
        vo.setUpdateUserName(p.getUpdateUserName());
        vo.setCreateTime(p.getCreateTime());
        vo.setCreateTimeAgo(Dates.ago(p.getCreateTime()));
        return vo;
    }

    static {
        TypeStore.STORE.register(HistoryValueSnapshotDO.class, HistoryValueVO.class, p -> {
            HistoryValueVO vo = new HistoryValueVO();
            vo.setId(p.getId());
            vo.setType(p.getOperatorType());
            vo.setBeforeValue(p.getBeforeValue());
            vo.setAfterValue(p.getAfterValue());
            vo.setUpdateUserId(p.getUpdateUserId());
            vo.setUpdateUserName(p.getUpdateUserName());
            vo.setCreateTime(p.getCreateTime());
            vo.setCreateTimeAgo(Dates.ago(p.getCreateTime()));
            return vo;
        });
    }
}
